package org.dromara.book.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.book.domain.bo.SzlyBaseLocationBo;
import org.dromara.book.domain.vo.SzlyBaseLocationVo;
import org.dromara.book.domain.SzlyBaseLocation;
import org.dromara.book.mapper.SzlyBaseLocationMapper;
import org.dromara.book.service.ISzlyBaseLocationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 馆藏地Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-19
 */
@RequiredArgsConstructor
@Service
public class SzlyBaseLocationServiceImpl implements ISzlyBaseLocationService {

    private final SzlyBaseLocationMapper baseMapper;

    /**
     * 查询馆藏地
     */
    @Override
    public SzlyBaseLocationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询馆藏地列表
     */
    @Override
    public TableDataInfo<SzlyBaseLocationVo> queryPageList(SzlyBaseLocationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SzlyBaseLocation> lqw = buildQueryWrapper(bo);
        Page<SzlyBaseLocationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询馆藏地列表
     */
    @Override
    public List<SzlyBaseLocationVo> queryList(SzlyBaseLocationBo bo) {
        LambdaQueryWrapper<SzlyBaseLocation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SzlyBaseLocation> buildQueryWrapper(SzlyBaseLocationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SzlyBaseLocation> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SzlyBaseLocation::getName, bo.getName());
        if (StringUtils.isNotBlank(bo.getCode())){
            lqw.eq(SzlyBaseLocation::getCode, bo.getCode().toUpperCase());
        }
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SzlyBaseLocation::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增馆藏地
     */
    @Override
    public Boolean insertByBo(SzlyBaseLocationBo bo) {
        SzlyBaseLocation add = MapstructUtils.convert(bo, SzlyBaseLocation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改馆藏地
     */
    @Override
    public Boolean updateByBo(SzlyBaseLocationBo bo) {
        SzlyBaseLocation update = MapstructUtils.convert(bo, SzlyBaseLocation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SzlyBaseLocation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除馆藏地
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
